package qjc.z_排序;

import java.util.Arrays;

public class z5_希尔排序 {
    /**
     * 分组插入，每组元素间隙为gap，每轮排序后缩小gap，直至gap为1
     *
     * @param nums
     */
    private static void sort(int[] nums) {
        for (int gap = nums.length >>> 1; gap > 0; gap = gap >>> 1) {
            for (int low = gap; low < nums.length; low++) {
                int t = nums[low];
                int i = low - gap;
                while (i >= 0 && t < nums[i]) {
                    nums[i + gap] = nums[i];
                    i -= gap;
                }
                if (i != low - gap) {
                    nums[i + gap] = t;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] a = {9, 3, 7, 2, 5, 8, 1, 4};
        System.out.println(Arrays.toString(a));
        sort(a);
        System.out.println(Arrays.toString(a));
    }

}
